<?php

namespace App\Http\Controllers;

use App\Models\Icp;
use App\Models\Site;
use GuzzleHttp\Client;

use Illuminate\Http\Request;

class ApiController extends Controller
{
    //

    protected $token;
    public function __construct() {
        $this->token    = 'Bearer '.env('TOKEN');
    }
    public function apiUrl($str)
    {
        return env('SEO_API_URL').$str;
    }

    public function icp($id)
    {
        $site    = Site::where('id',$id)->first();
        $client = new Client;
        $res = $client->request('POST', $this->apiUrl('icp'),[
            'headers' => [
                'Authorization' => $this->token,
            ],
            'form_params' => [
                'name'  => $site->url,
            ]
        ]);
        if($res->getStatusCode() != 200){
            return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
        }else{
            $response   = json_decode($res->getBody());
            if($response->code == 0) {
                $flight = Icp::updateOrCreate(
                    ['site_id' => $id],
                    [
                        'site_id'       => $id,
                        'name'          => $response->body->name,
                        'type'          => $response->body->type,
                        'now_icp'       => $response->body->now_icp,
                        'check_date'    => $response->body->check_date ? $response->body->check_date : date('Y-m-d', time()),
                    ]
                );
                return response()->json(['msg' => '数据已经获取成功!3秒后,刷新页面', 'code' => '0'],200);
            }else{
                return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
            }
        }
    }
    public function seo($id)
    {
        $site    = Site::where('id',$id)->first();
        if(!$site){
            return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
        }
        $client = new Client;
        $res = $client->request('POST', $this->apiUrl('rank'),[
            'headers' => [
                'Authorization' => $this->token,
            ],
            'form_params' => [
                'name'  => $site->url,
            ]
        ]);
        if($res->getStatusCode() != 200){
            return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
        }else{
            $response   = json_decode($res->getBody());
            if($response->code == 0) {
                $array  = [
                    'ip'    => $response->body->ip,
                    'alexa' => $response->body->alexa,
                    'br' => $response->body->br,
                    'mbr' => $response->body->mbr,
                    'pr' => $response->body->pr,
                    'sbr' => $response->body->sbr,
                ];
                $flight = Site::find($id);
                $flight->content = $array;
                $flight->save();
                return response()->json(['msg' => '数据已经获取成功!3秒后,刷新页面', 'code' => '0'],200);
            }else{
                return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
            }
        }
    }
    public function rank(Request $request)
    {
        $client = new Client;
        $res = $client->request('POST', $this->apiUrl('rank'),[
            'headers' => [
                'Authorization' => $this->token,
            ],
            'form_params' => [
                'name'  => $request->get('name'),
            ]
        ]);
        if($res->getStatusCode() != 200){
            return response()->json(['data' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => 0],200);
        }else{
            $response   = json_decode($res->getBody());
            if($response->code == 0) {
                return response()->json(['data' => [
                    'ip'    => $response->body->ip,
                    'alexa' => $response->body->alexa,
                    'br' => $response->body->br,
                    'mbr' => $response->body->mbr,
                    'pr' => $response->body->pr,
                    'sbr' => $response->body->sbr,
                ], 'code' => 1],200);
            }else{
                return response()->json(['data' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => 0],200);
            }
        }
    }
    public function snapshot($id)
    {
        $site    = Site::where('id',$id)->first();
        if(!$site){
            return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
        }
        $client = new Client;
        $res = $client->request('POST', $this->apiUrl('shot'),[
            'headers' => [
                'Authorization' => $this->token,
            ],
            'form_params' => [
                'name'  => $site->url,
            ]
        ]);
        if($res->getStatusCode() != 200){
            return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
        }else{
            $response   = json_decode($res->getBody());
            if($response->code == 0) {
                return response()->json(['msg' => '数据已经获取成功!3秒后,刷新页面', 'code' => '0'],200);
            }else{
                return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
            }
        }
    }
    public function webSiteShot($urls)
    {
        $array = [
            'headers' => [
                'Authorization' => $this->token,
            ],
            'form_params' => [
                'name'  => $urls,
            ]
        ];
        $client = new Client;
        $res = $client->request('POST', $this->apiUrl('shot') , $array);
        if($res->getStatusCode() != 200){
            return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
        }else{
            $response   = json_decode($res->getBody());
            if($response->code == 0) {
                return response()->json(['msg' => '数据已经获取成功!3秒后,刷新页面', 'code' => '0'],200);
            }else{
                return response()->json(['msg' => '您需要的数据!出现问题,正在修理过程中……请稍候重试!……', 'code' => '1'],200);
            }
        }
    }
}
